package com.itheima.controller;

import cn.hutool.log.Log;
import com.itheima.exception.AuthorizationFailException;
import com.itheima.interceptors.LoginInterceptor;
import com.itheima.pojo.Category;
import com.itheima.pojo.Result;
import com.itheima.service.ArticleService;
import com.itheima.service.CategoryService;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @PostMapping
    public Result<Category> addCategory(@RequestBody Category category) {
        category.setCreateUser(LoginInterceptor.curUser.get().getId());
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        categoryService.addCategory(category);
        return Result.success(category);
    }

    @GetMapping
    public Result<List<Category>> getCategory() {
        Integer id = LoginInterceptor.curUser.get().getId();
        List<Category> category = categoryService.getCategory(id);
        return Result.success(category);
    }

    @GetMapping("/detail")
    public Result<Category> getCategoryDetailById(Integer id) {
        Category category = categoryService.getDetailById(id);
        return Result.success(category);
    }

    @PutMapping
    public Result<Category> updateCategoryById(@RequestBody Category category) {
        Category ca = categoryService.updateCategoryById(category);
        return Result.success(ca);
    }

    @DeleteMapping
    public Result<Category> deleteCategory(Integer id){
        Category category = categoryService.getDetailById(id);
        categoryService.deleteCategory(id);
        return Result.success(category);
    }
}
